using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Text.RegularExpressions;

namespace Common.IO.FileIO
{
    public enum IndentifyResult
    {
        IndenfitySuccess = 0,
        ExcelNotExist = 1,
        ExcelNameWrong = 2,
        ExcelFormatWrong = 3,
        DataFormatWrong = 4
    };

    public class ExcelIndentify
    {
        public string ExcelFilePath;
        public ExcelIndentify(string excelfilepath)
        {
            this.ExcelFilePath = excelfilepath;
        }

        public IndentifyResult FileIndentify(string regexpression, string[] columnnames)
        {
            if (!File.Exists(this.ExcelFilePath))
                return IndentifyResult.ExcelNotExist;

            string filename = (new FileInfo(this.ExcelFilePath)).Name;
            Regex reg = new Regex(regexpression);
            if (!reg.IsMatch(filename))
                return IndentifyResult.ExcelNameWrong;

            ExcelIO excel = new ExcelIO(this.ExcelFilePath);
            DataSet ds = excel.GetData();
            if (ds.Tables[0].Columns.Count != columnnames.Length)
                return IndentifyResult.ExcelFormatWrong;
            for(int i = 0; i < columnnames.Length; i++)
            {
                if (ds.Tables[0].Columns[i].ColumnName.Trim() != columnnames[i])
                    return IndentifyResult.ExcelFormatWrong;
            }

            return IndentifyResult.IndenfitySuccess;
        }

        public IndentifyResult FileIndentify(string[] columnnames)
        {
            if (!File.Exists(this.ExcelFilePath))
                return IndentifyResult.ExcelNotExist;

            ExcelIO excel = new ExcelIO(this.ExcelFilePath);
            DataSet ds = excel.GetData();
            if (ds.Tables[0].Columns.Count != columnnames.Length)
                return IndentifyResult.ExcelFormatWrong;
            for (int i = 0; i < columnnames.Length; i++)
            {
                if (ds.Tables[0].Columns[i].ColumnName.Trim() != columnnames[i])
                    return IndentifyResult.ExcelFormatWrong;
            }


            return IndentifyResult.IndenfitySuccess;
        }

        
    }
}
